using System;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;

namespace Curriculum.UI.Mensajes
{

    interface IMessageHandler
    {
        void HandleError(Exception ex);
        void HandleError(Exception ex, Form form);
        void ShowInfoMessage(string text);
        bool ShowQuestionMessage(string text);
        
    }

    class MessageHandlerFactory
    {
        private static MessageHandlerFactory _instance = null;
        public static MessageHandlerFactory Instance
        {
            get { return _instance; }
        }

        static MessageHandlerFactory()
        {
            _instance = new MessageHandlerFactory();
        }

        private MessageHandlerFactory()
        {
            LoadConfig();
        }

        private void LoadConfig()
        {
            _handlerType = typeof(MessageHandler);//TODO, usar app.config
        }

        private Type _handlerType = null;
        private string _strHandlerType = string.Empty;
        private IMessageHandler _instanceHandler = null;
        public IMessageHandler GetHandler()
        {          
			System.Configuration.ConfigurationSettings.GetConfig("appmessages");
            //ConfigurationManager.RefreshSection("appmessages");
            //NameValueCollection c = (NameValueCollection)ConfigurationManager.GetSection("appmessages");
			NameValueCollection c = (NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig("appmessages");

           if (_strHandlerType != c["msg"])
           {
               _strHandlerType = c["msg"];
               _instanceHandler = null;
               //_handlerType = Type.GetType(_strHandlerType);
			   LoadConfig();
           }            
            if (_instanceHandler == null)
                _instanceHandler = (IMessageHandler)Activator.CreateInstance(_handlerType);        
           return _instanceHandler;
        }
    }

    class MessageHandler : IMessageHandler
    {

        public MessageHandler()
        { 
        }

        public void HandleError(Exception ex)
        {
            MessageBox.Show(ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void HandleError(Exception ex, Form form)
        {
            MessageBox.Show(form, ex.Message);
        }

        public void ShowInfoMessage(string text)
        {
            MessageBox.Show(text,string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public bool ShowQuestionMessage(string text)
        {
           return DialogResult.OK == MessageBox.Show(text, string.Empty, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        }
    }

    class MessageHandlerDos : IMessageHandler
    {

        public MessageHandlerDos()
        {
        }

        public void HandleError(Exception ex)
        {
            MessageBox.Show(ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void HandleError(Exception ex, Form form)
        {
            MessageBox.Show(form, ex.Message);
        }

        public void ShowInfoMessage(string text)
        {
            MessageBox.Show(text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public bool ShowQuestionMessage(string text)
        {
            return DialogResult.OK == MessageBox.Show(text, string.Empty, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        }
    }
}
